﻿namespace EasyWeb.Web.UI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Mvc;

    public class EWGridBuilder<TModel> : EWViewComponentBuilder<EWGrid<TModel>, EWGridBuilder<TModel>>
    {
        private EWGridAjaxBuilder<TModel> ajaxBuilder;
        private EWGridClientEventBuilder<TModel> clientEventBuilder;
        private EWGridColumnBuilder<TModel> columnsBuilder;
        private EWGridDataBindingBuilder<TModel> dataBindingBuilder;
        private EWGridPagingBuilder<TModel> pagingBuilder;
        private EWGridSortingBuilder<TModel> sortingBuilder;
        private EWGridStyleBuilder<TModel> styleBuilder;

        public EWGridBuilder(EWGrid<TModel> grid)
            : base(grid)
        {
        }

        public EWGridBuilder<TModel> Ajax(Action<EWGridAjaxBuilder<TModel>> action)
        {
            EWGuard.IsNotNull(action, "action");
            if (this.ajaxBuilder == null)
            {
                this.ajaxBuilder = new EWGridAjaxBuilder<TModel>(this.Component);
            }
            action(this.ajaxBuilder);
            return this;
        }

        public EWGridBuilder<TModel> ClientEvents(Action<EWGridClientEventBuilder<TModel>> action)
        {
            EWGuard.IsNotNull(action, "action");
            if (this.clientEventBuilder == null)
            {
                this.clientEventBuilder = new EWGridClientEventBuilder<TModel>(this.Component);
            }
            action(this.clientEventBuilder);
            return this;
        }

        public EWGridBuilder<TModel> Columns(Action<EWGridColumnBuilder<TModel>> action)
        {
            EWGuard.IsNotNull(action, "action");
            if (this.columnsBuilder == null)
            {
                this.columnsBuilder = new EWGridColumnBuilder<TModel>(this.Component);
            }
            action(this.columnsBuilder);
            this.Component.Columns = this.columnsBuilder.Columns.ToArray();
            return this;
        }

        public EWGridBuilder<TModel> DataBind(Action<EWGridDataBindingBuilder<TModel>> action)
        {
            EWGuard.IsNotNull(action, "action");
            if (this.dataBindingBuilder == null)
            {
                this.dataBindingBuilder = new EWGridDataBindingBuilder<TModel>(this.Component);
            }
            action(this.dataBindingBuilder);
            return this;
        }

        public EWGridBuilder<TModel> Pageable(Action<EWGridPagingBuilder<TModel>> action = null)
        {
            this.Component.CanPage = true;
            if (action != null)
            {
                if (this.pagingBuilder == null)
                {
                    this.pagingBuilder = new EWGridPagingBuilder<TModel>(this.Component);
                }
                action(this.pagingBuilder);
            }
            return this;
        }

        public EWGridBuilder<TModel> Sortable(Action<EWGridSortingBuilder<TModel>> action = null)
        {
            this.Component.CanSort = true;
            if (action != null)
            {
                if (this.sortingBuilder == null)
                {
                    this.sortingBuilder = new EWGridSortingBuilder<TModel>(this.Component);
                }
                action(this.sortingBuilder);
            }
            return this;
        }

        public EWGridBuilder<TModel> Style(Action<EWGridStyleBuilder<TModel>> action)
        {
            EWGuard.IsNotNull(action, "action");
            if (this.styleBuilder == null)
            {
                this.styleBuilder = new EWGridStyleBuilder<TModel>(this.Component);
            }
            action(this.styleBuilder);
            return this;
        }

        public EWGridBuilder<TModel> AutoSortAndPage(bool value = true)
        {
            this.Component.AutoSortAndPage = value;
            return this;
        }

        public EWGridBuilder<TModel> Caption(string value)
        {
            this.Component.Caption = value;
            return this;
        }

        public EWGridBuilder<TModel> DataKey(string value)
        {
            this.Component.DataKey = value;
            return this;
        }

        public EWGridBuilder<TModel> DataKey<TKey>(Expression<Func<TModel, TKey>> value)
        {
            this.Component.DataKey = value == null ? null : ExpressionHelper.GetExpressionText(value);
            return this;
        }

        public EWGridBuilder<TModel> DisplayHeader(bool value)
        {
            this.Component.DisplayHeader = value;
            return this;
        }

        public EWGridBuilder<TModel> FillEmptyRows(bool value)
        {
            this.Component.FillEmptyRows = value;
            return this;
        }

        public EWGridBuilder<TModel> EmptyRowCellValue(string value)
        {
            this.Component.EmptyRowCellValue = value;
            return this;
        }

        public EWGridBuilder<TModel> FieldNamePrefix(string value)
        {
            this.Component.FieldNamePrefix = value;
            return this;
        }

        public EWGridBuilder<TModel> SelectionFieldName(string value)
        {
            this.Component.SelectionFieldName = value;
            return this;
        }
    }
}
